/***************************firefoxupdater.cpp******************************/
/*****************************************************************************/
/*******************Browsers Daily Build Updater************************/
/************Copyright (C) 2010-2011  Gianluigi Biancucci*************/
/*****************************************************************************/
/*****This is free software, and you are welcome to redistribute it****/
/*****and / or modify underthe terms of GPLV2 License.**************/
/*****This program comes with ABSOLUTELY NO WARRANTY*****/
/*****************************************************************************/

#include "FirefoxUpdater.h"
#include "singletondownloader.h"
#include "iostream"
#include "QFile"
#include <QMessageBox>
#include <QIcon>
#include <QLabel>
#include <QDir>
#include <QProcess>
#include <QStringList>
#include "firefoxupdater.h"
#include "paths.h"
#include <QVariant>
FirefoxUpdater::FirefoxUpdater(QString arch)

{


    downloader = SingletonDownloader::getInstance();
    QDir dir;
    dir = QDir();
    if(!dir.exists(FFOXPATH+"/"))dir.mkdir(FFOXPATH+"/");
    check64=new QFile(FFOXPATH+"/check64.txt");
    check32=new QFile(FFOXPATH+"/check32.txt");
    _64="win64-x86_64";
    _32="win32";
    this->setArch(arch);
    brenchNum = "";
    BName = "Firefox";
    lastError ="No Error";

}



void FirefoxUpdater::setArch(QString arch){

    if(arch == "32"){
        currCheckFile=check32;
        currOsType=_32;
    }
    else {
        currCheckFile=check64;
        currOsType=_64;
    }
}


bool FirefoxUpdater::download(QString version, QString * exePath){

    //Path 64 bit http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/firefox-4.0b8pre.en-US.win64-x86_64.installer.exe
    //Path 32 bit http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/firefox-4.0b8pre.en-US.win32.installer.exe

    QByteArray firefox;
    if( downloader->syncGet("http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/firefox-"+brenchNum+"."+currOsType+".installer.exe",&firefox)){
        QString tStamp=version;
        QFile* ffile = new QFile(FFOXPATH+"/firefox_"+currOsType+"_"+tStamp.remove(14,tStamp.length()-14)+".exe");
        ffile->open(QIODevice::WriteOnly);
        ffile->write(firefox);
        ffile->close();
        delete ffile;
        currCheckFile->open(QIODevice::WriteOnly);
        QVariant v = version;
        currCheckFile->write(v.toByteArray());
        currCheckFile->close();
        *exePath =  FFOXPATH+"/firefox_"+currOsType+"_"+tStamp+".exe";
        return true;
    } else {
        lastError ="Undefined error when downloading Firefox installer.\nCheck your connection settings and try again.";
        return false;
    }
}




bool  FirefoxUpdater::getLatestVersion(QString * lastVer){

    QByteArray  res;
    if(!downloader->syncGet("http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/",&res)){
        lastError ="Undefined error when downloading nighty build page of Firefox.\nCheck your connection settings and try again.";
        return false;
    }
    QString page(res);
    brenchNum=this->getContentBetween(page,"firefox-","."+currOsType+".txt");
    QByteArray verBA;
    if(!downloader->syncGet("http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/firefox-"+brenchNum+"."+currOsType+".txt",&verBA)){

        lastError ="Undefined error when checking the last version od Firefox.\nCheck your connection settings and try again.";
        return false;

    }
    QString ver(verBA);
    *lastVer=ver;
    return true;
}

bool  FirefoxUpdater::getInstalledVersion(QString * installedVer){
    if(!currCheckFile->exists()){
        currCheckFile->open(QIODevice::WriteOnly);
        currCheckFile->close();
    }
    if(currCheckFile->open(QIODevice::ReadOnly)){
        QString ver(currCheckFile->readAll());
        currCheckFile->close();
        *installedVer=ver;
        return true;
    }
    else {
        lastError="unable to open Firefox settings file ";
        return false;
    }
}

QString FirefoxUpdater::getContentBetween(QString source, QString startStr, QString endStr){

    int end = source.indexOf(endStr);
    if(end != -1){
        source = source.remove(end,source.length()-(end));
        int start =0;
        QString sourcecopy = source;
        while (start != -1){
            start = sourcecopy.indexOf(startStr);
            if (start != -1) {
                sourcecopy=sourcecopy.remove(0,start+startStr.length());
                source = sourcecopy;
            }
        }
    }
    return source;
}

QString FirefoxUpdater::getBrowserName(){

    return BName;

}

QString FirefoxUpdater::getLastError(){

    return lastError;

}
